package com.gitee.tangsixiang.springbootdisruptorsample;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/***
 @author tangsixiang@hotmail.com
 描述：
 @date 2023年07月27日 9:33

 */
public class PhoneUtil {



    /**
     * main
     * @param args
     */
    public static void main(String[] args) {
        String val = "18888888888";
        String result=encryptPhone(val);
        System.out.println(result);

    }

    /**
     * @param val
     * @return 数字转字母
     */
    public static String getPhoneStr(String val) {
        Integer integer = RandomUtil.randomInt(10);
        String returnStr = "";
        if (val != null && val.length() > 0) {
            byte[] obj = val.getBytes();
            for (int i = 0; i < obj.length; i++) {
                returnStr += getLetter(integer,String.valueOf(val.charAt(i)));
            }
        }
        return returnStr;
    }

    /***
     * 换位号码中两组数字：第二位和第六位交换，第三位和第五位交换，位数号按从左往右数
     * 13510642584
     * 16015342584
     */
    public static String changeNum(String val){

        if(!StrUtil.isEmpty(val)){
            String str="";
            char [] phone=val.toCharArray();
            for(int i=0;i<phone.length;i++){
                if(i==1||i==2||i==4||i==5){
                    if(i==1)
                        str+=phone[5];
                    if(i==2)
                        str+=phone[4];
                    if(i==4)
                        str+=phone[2];
                    if(i==5)
                        str+=phone[1];
                }else{
                    str+=phone[i];
                }
            }
            return str;
        }
        return val;
    }

    /**
     * 通过Map值获取key
     * @return
     */
    public static String getLetter(Integer phoneNum,String val) {
        for(Map.Entry<String,String> entry:getMapData(phoneNum).entrySet()){
            if(val.equals(entry.getValue()))
                return String.valueOf(entry.getKey());
        }
        return val;
    }

    /**
     * @return Map
     */
    public static Map<String,String> getMapData(Integer value){
        if(value<3){
            return MapUtil.builder(new HashMap<String, String>()).put("w", "1").put("a", "2").put("s", "3").put("t", "4")
                    .put("u", "5").put("h", "6").put("v", "7").put("y", "8").put("z", "9").put("o", "0").build();
        }else if(value<7){
            return MapUtil.builder(new HashMap<String, String>()).put("l", "1").put("g", "2").put("c", "3").put("p", "4")
                    .put("x", "5").put("j", "6").put("r", "7").put("q", "8").put("i", "9").put("m", "0").build();
        }else {
            return MapUtil.builder(new HashMap<String, String>()).put("k", "1").put("n", "2").put("a", "3").put("p", "4")
                    .put("x", "5").put("h", "6").put("v", "7").put("y", "8").put("i", "9").put("o", "0").build();
        }
    }



    /**
     * 字符串前增加 prefix
     */
    public static String prefixStr(String prefix,String val){
        return prefix+val;
    }


    /**
     * 随机插入3个数字
     */
    public static String insertNum(String val){
        String num=radomNum(3);
        //往指定长度字符串加入随机数字
        for(int i=0;i<num.length();i++){
            Integer inNum= Integer.parseInt(radomNum(1));
            String result=val.substring(0,inNum>val.length()?val.length():inNum)+num.charAt(i)+val.substring(inNum>val.length()?val.length():inNum,val.length());
            val=result;
        }
        return val;
    }


    /**
     * 随机生成i位数字
     * i<10
     */
    public static String radomNum(int i){
        Random random = new Random();
        String val=String.valueOf(random.nextInt());
        return val.substring(0,i).indexOf("-")>-1?radomNum(i):val.substring(0,i);
    }


    public static String encryptPhone(String phoneStr){

        if(NumberUtil.isNumber(phoneStr)){
            //1.换位号码中两组数字：第二位和第六位交换，第三位和第五位交换
            phoneStr=changeNum(phoneStr);
            //2.任意位置插入三个随机数字
            phoneStr=insertNum(phoneStr);
            //3.将全部号码转换为对应的字符
            phoneStr=getPhoneStr(phoneStr);
            System.out.println("phoneStr = " + phoneStr);
            //4.在步骤3之后的字符串前加上”P”
            phoneStr=prefixStr("e",phoneStr);
            return phoneStr;
        }else {
            return     IdUtil.simpleUUID();
        }


    }
}
